{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "75417ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72f50802",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-07-01 00:51:04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-07-01 00:46:04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.16</td>\n",
       "      <td>20.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-07-01 00:25:09</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.80</td>\n",
       "      <td>70.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-07-01 00:33:32</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.46</td>\n",
       "      <td>66.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-07-01 00:00:55</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.70</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime  passenger_count  trip_distance  total_amount\n",
       "0  2019-07-01 00:51:04              1.0           0.00          4.94\n",
       "1  2019-07-01 00:46:04              1.0           4.16         20.30\n",
       "2  2019-07-01 00:25:09              1.0          18.80         70.67\n",
       "3  2019-07-01 00:33:32              1.0          18.46         66.36\n",
       "4  2019-07-01 00:00:55              0.0           1.70         15.30"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load taxi data from January 2019 into a data frame, \n",
    "# using only the columns `tpep_pickup_datetime`, `passenger_count`, `trip_distance`, and `total_amount`, \n",
    "# making sure to load `tpep_pickup_datetime` as `datetime`.\n",
    "\n",
    "filename = '../data/nyc_taxi_2019-07.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['tpep_pickup_datetime', 'trip_distance', 'passenger_count', 'total_amount'],\n",
    "                 parse_dates=['tpep_pickup_datetime'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c5c7133",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Export, to a tab-delimited CSV file, the columns tpep_pickup_datetime, passenger_count, trip_distance, total_amount\n",
    "# Use the format day/month/year HHs:MMm:SSs\n",
    "\n",
    "dt_format='%d/%m/%Y %Hh:%Mm:%Ss'\n",
    "\n",
    "df.to_csv('ex40_taxi_07_2019.csv',\n",
    "         sep='\\t',\n",
    "         columns=['tpep_pickup_datetime', 'passenger_count', 'trip_distance', 'total_amount'],\n",
    "         date_format=dt_format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0a2f305b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read the CSV file that we just created into a data frame. \n",
    "# make sure to parse the datetime column (tpep_pickup_datetime) in its current format\n",
    "\n",
    "import time\n",
    "\n",
    "def parse_weird_format(s):\n",
    "    return time.strptime(s, dt_format)\n",
    "\n",
    "df = pd.read_csv('ex40_taxi_07_2019.csv',\n",
    "           sep='\\t',\n",
    "           usecols=['tpep_pickup_datetime', 'passenger_count', 'trip_distance', 'total_amount'],\n",
    "           parse_dates=['tpep_pickup_datetime'],\n",
    "           date_parser=parse_weird_format)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d2f10aeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime     object\n",
       "passenger_count         float64\n",
       "trip_distance           float64\n",
       "total_amount            float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check that the 'export_date' column is indeed a datetime\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f24a699",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
